package com.hl.sfzy.controller;

import com.hl.sfzy.constant.CommonConstant;
import com.hl.sfzy.constant.Result;
import com.hl.sfzy.entity.SysCompany;

import com.hl.sfzy.entity.SysUser;

import com.hl.sfzy.service.SyscompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class SysCompanyController {
    @Autowired
    private SyscompanyService syscompanyService;

    @PostMapping("/sysCompany")
    public Result add(@RequestBody SysCompany sysCompany) {
        int num = syscompanyService.add(sysCompany);
        return num > 0 ? Result.success() : Result.error();
    }

    /*
     * 删除
     * */
    @DeleteMapping("/sysCompany/{id}")
    public Result remove(@PathVariable("id") Integer id) {
        int num = syscompanyService.remove(id);
        return num > 0 ? Result.success() : Result.error();
    }

    @DeleteMapping("/sysCompany")
    public Result removeBatch(Integer[] ids) {
        int num = syscompanyService.removeBatch(ids);
        return num > 0 ? Result.success() : Result.error();

    }

    @PutMapping("/sysCompany")
    public Result edit(@RequestBody SysCompany sysCompany) {
        int num = syscompanyService.edit(sysCompany);
        return num > 0 ? Result.success() : Result.error();
    }

    @GetMapping("/sysCompany/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        SysCompany sysCompany = syscompanyService.findById(id);
        return Result.success(sysCompany);
    }

    @GetMapping("/sysCompany")
    public Result findAll(SysCompany sysCompany) {
        List<SysCompany> list = syscompanyService.find(sysCompany);
        return Result.success(list);
    }

    @GetMapping("/sysCompany/page")
    public Result findPage(
            @RequestParam(value = CommonConstant.PAGE_NUM, required = false, defaultValue = CommonConstant.PAGE_NUM_DEFAULT) Integer pageNum,
            @RequestParam(value = CommonConstant.PAGE_SIZE, required = false, defaultValue = CommonConstant.PAGE_SIZE_DEFAULT) Integer pageSize,
            SysCompany sysCompany) {

        Map<String, Object> map = syscompanyService.findPage(pageNum, pageSize, sysCompany);
        return Result.success(map);
    }

    @GetMapping("/sysCompany/byName/{companyname}")
    public Result findByUsername(@PathVariable("companyname") String companyname){
        SysCompany sysCompany = syscompanyService.selectByCompanyname(companyname);
        return Result.success(sysCompany);
    }

    @GetMapping("/sysCompany/count")
    public  Result findCount(){
        int count = syscompanyService.findCount();
        return Result.success(count);
    }
}
